import jwt
from datetime import datetime
from datetime import timedelta
from django.conf import settings
class JWTTool:
    #生成token payload用户信息
    def get_token(self, userid):
        # 需要一个过期时间
        exp = datetime.utcnow() + timedelta(days=1)
        payload = {
            "userid": userid,
            "exp": exp
        }
        key = settings.SECRET_KEY
        return "Bearer " + jwt.encode(key=key, payload=payload, algorithm="HS256")

    #解析token
    def checktoken(self,token):
        return jwt.decode(token,settings.SECRET_KEY,algorithms=["HS256"])
tooljwt=JWTTool()
# payload={"userid":1,"username":"admin"}
# print(tool.get_token(payload))
